home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / sd-26.zip / sdsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-09  |  9.1 KB  |  415 lines

  1. /* SD -- square dance caller's helper.
  2.  
  3.     Copyright (C) 1990, 1991, 1992  William B. Ackerman.
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 1, or (at your option)
  8.     any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     This is for version 25. */
  20.  
  21. /* This defines the following functions:
  22.    general_initialize
  23.    generate_random_number
  24.    generate_random_concept_p
  25.    get_mem
  26.    free_mem
  27.    get_date
  28.    open_file
  29.    probe_file
  30.    write_file
  31.    close_file
  32.    print_line
  33.    print_id_error
  34.    init_error
  35.    add_resolve_indices
  36.    final_exit
  37.    open_database
  38.    read_from_database
  39.    close_database
  40.    fill_in_neglect_percentage
  41.    parse_number
  42.    open_call_list_file
  43.    read_from_call_list_file
  44.    write_to_call_list_file
  45.    close_call_list_file
  46.  
  47. and the following external variables:
  48.    random_number
  49.    hashed_randoms
  50. */
  51.  
  52.  
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <string.h>
  56. #include <time.h>
  57.  
  58. /* We take pity on those poor souls who are compelled to use
  59.     troglodyte development environments. */
  60.  
  61. #ifdef __STDC__
  62. #include <stdlib.h>
  63. #else
  64. extern void free(void *ptr);
  65. extern char *malloc(unsigned int siz);
  66. extern void exit(int code);
  67. extern void srand48(long int);
  68. extern long int lrand48(void);
  69. #endif
  70.  
  71. #include "sd.h"
  72. #include "paths.h"
  73.  
  74.  
  75. /* These variables are external. */
  76.  
  77. int random_number;
  78. int hashed_randoms;
  79.  
  80.  
  81. static int last_hashed_randoms;
  82. static FILE *fp;
  83. static FILE *fildes;
  84. static long_boolean file_error;
  85. static char fail_message[MAX_ERR_LENGTH];
  86. static char fail_errstring[MAX_ERR_LENGTH];
  87.  
  88.  
  89. static char * get_errstring(void)
  90. {
  91. #ifdef sun
  92.    extern int sys_nerr;
  93.    extern char *sys_errlist[];
  94.  
  95.    if (errno < sys_nerr) return sys_errlist[errno];
  96.    else return "?unknown error?";
  97. #else
  98.    return strerror(errno);
  99. #endif
  100. }
  101.  
  102.  
  103. extern void general_initialize(void)
  104. {
  105.    srand48(time((long int *)0));
  106. }
  107.  
  108.  
  109. extern int generate_random_number(int modulus)
  110. {
  111.    int j;
  112.  
  113.    random_number = lrand48();
  114.    j = random_number % modulus;
  115.    last_hashed_randoms = hashed_randoms;       /* save in case we need to undo it */
  116.    hashed_randoms = hashed_randoms*37+j;
  117.    return(j);
  118. }
  119.  
  120.  
  121. extern long_boolean generate_random_concept_p(void)
  122. {
  123.    int i = generate_random_number(8);
  124.  
  125.    /* Since we are not going to use the random number in a one-to-one way, we run the risk
  126.       of not having hashed_randoms uniquely represent what is happening.  To remedy
  127.       the problem, we undo the transformation that was just made to hashed_randoms,
  128.       and redo it with just the information we intend to use. */
  129.  
  130.    hashed_randoms = last_hashed_randoms*37;       /* Now just need to add something. */
  131.  
  132.    if (i < CONCEPT_PROBABILITY) {
  133.       hashed_randoms++;          /* This should do the trick. */
  134.       return (TRUE);
  135.    }
  136.    else {
  137.       return (FALSE);
  138.    }
  139. }
  140.  
  141.  
  142. extern void *get_mem(unsigned int siz)
  143. {
  144.    void *buf;
  145.  
  146.    buf = malloc(siz);
  147.    if (!buf) {
  148.       fprintf(stderr, "Can't allocate %d bytes of memory.\n", siz);
  149.       perror("malloc");
  150.       exit_program(2);
  151.    }
  152.    return(buf);
  153. }
  154.  
  155.  
  156. extern void free_mem(void *ptr)
  157. {
  158.    free(ptr);
  159. }
  160.  
  161.  
  162. extern void get_date(char dest[])
  163. {
  164.    long int clock;
  165.    char *junk;
  166.    char *dstptr;
  167.  
  168.    time(&clock);
  169.    junk = ctime(&clock);
  170.    dstptr = dest;
  171.    string_copy(&dstptr, junk);
  172.    if (dstptr[-1] == '\n') dstptr[-1] = '\0';         /* Stupid UNIX! */
  173. }
  174.  
  175.  
  176.  
  177. extern void open_file(void)
  178. {
  179.    int this_file_position;
  180.  
  181.    file_error = FALSE;
  182.  
  183.    if (!(fildes = fopen(outfile_string, "a"))) {
  184.       (void) strncpy(fail_errstring, get_errstring(), MAX_ERR_LENGTH);
  185.       (void) strncpy(fail_message, "open", MAX_ERR_LENGTH);
  186.       file_error = TRUE;
  187.       return;
  188.    }
  189.  
  190.    this_file_position = ftell(fildes);
  191.  
  192.    if ((last_file_position != -1) && (last_file_position != this_file_position)) {
  193.       writestuff("Warning -- file has been modified since last sequence.");
  194.       newline();
  195.       newline();
  196.    }
  197.  
  198.    if (this_file_position == 0) {
  199.       writestuff("File does not exist, creating it.");
  200.       newline();
  201.       newline();
  202.    }
  203.    else {
  204.       char junk[1];
  205.  
  206.       if (last_file_position == -1) {
  207.          writestuff("Appending to existing file.");
  208.          newline();
  209.          newline();
  210.       }
  211.  
  212.       junk[0] = '\f';      /* Write a formfeed (end-of-page indicator). */
  213.       if ((fwrite(junk, 1, 1, fildes) != 1) || ferror(fildes)) {
  214.          (void) strncpy(fail_errstring, get_errstring(), MAX_ERR_LENGTH);
  215.          (void) strncpy(fail_message, "write", MAX_ERR_LENGTH);
  216.          file_error = TRUE;
  217.       }
  218.    }
  219. }
  220.  
  221.  
  222. extern long_boolean probe_file(char filename[])
  223. {
  224.    return (TRUE);       /* Can't probe in UNIX. */
  225. }
  226.  
  227.  
  228. extern void write_file(char line[])
  229. {
  230.    int size;
  231.    char nl = '\n';
  232.  
  233.    if (file_error) return;    /* Don't keep trying after a failure. */
  234.  
  235.    size = strlen(line);
  236.    if ((fwrite(line, 1, size, fildes) != size) || ferror(fildes)) {
  237.       (void) strncpy(fail_errstring, get_errstring(), MAX_ERR_LENGTH);
  238.       (void) strncpy(fail_message, "write", MAX_ERR_LENGTH);
  239.       file_error = TRUE;      /* Indicate that sequence will not get written. */
  240.       return;
  241.    }
  242.  
  243.    if ((fwrite(&nl, 1, 1, fildes) != 1) || ferror(fildes)) {
  244.       (void) strncpy(fail_errstring, get_errstring(), MAX_ERR_LENGTH);
  245.       (void) strncpy(fail_message, "write", MAX_ERR_LENGTH);
  246.       file_error = TRUE;      /* Indicate that sequence will not get written. */
  247.    }
  248. }
  249.  
  250.  
  251. extern void close_file(void)
  252. {
  253.    char foo[MAX_ERR_LENGTH*10];
  254.  
  255.    if (file_error) goto fail;
  256.    last_file_position = ftell(fildes);
  257.  
  258.    if (!fclose(fildes)) return;
  259.  
  260.    (void) strncpy(fail_errstring, get_errstring(), MAX_ERR_LENGTH);
  261.    (void) strncpy(fail_message, "close", MAX_ERR_LENGTH);
  262.  
  263.    fail:
  264.  
  265.    (void) strncpy(foo, "WARNING!!!  Sequence has not been written!  File ", MAX_ERR_LENGTH);
  266.    (void) strncat(foo, fail_message, MAX_ERR_LENGTH);
  267.    (void) strncat(foo, " failure on '", MAX_ERR_LENGTH);
  268.    (void) strncat(foo, outfile_string, MAX_ERR_LENGTH);
  269.    (void) strncat(foo, "': ", MAX_ERR_LENGTH);
  270.    (void) strncat(foo, fail_errstring, MAX_ERR_LENGTH);
  271.    specialfail(foo);
  272. }
  273.  
  274. /* Used only for printing error messages, so writes to stderr */
  275. extern void print_line(char s[])
  276. {
  277.    fprintf(stderr, "%s\n", s);
  278. }
  279.  
  280.  
  281. extern void print_id_error(int n)
  282. {
  283.    fprintf(stderr, "Call didn't identify self -- %d\n", n);
  284. }
  285.  
  286.  
  287. extern void init_error(char s[])
  288. {
  289.    fprintf(stderr, "Error initializing program: %s\n", s);
  290. }
  291.  
  292.  
  293. extern void add_resolve_indices(char junk[], int cur, int max)
  294. {
  295.    sprintf(junk, "%d out of %d", cur, max);
  296. }
  297.  
  298.  
  299.  
  300. extern void final_exit(int code)
  301. {
  302.    exit(code);
  303. }
  304.  
  305.  
  306.  
  307. extern void open_database(void)
  308. {
  309.    int format_version, n;
  310.  
  311.    if (!(fp = fopen(DATABASE_FILENAME, "r"))) {
  312.       fprintf(stderr, "Can't open database file.\n");
  313.       perror(DATABASE_FILENAME);
  314.       exit_program(1);
  315.    }
  316.  
  317.    if (read_from_database() != DATABASE_MAGIC_NUM) {
  318.       fprintf(stderr,
  319.          "Database file '%s' has improper format.\n", DATABASE_FILENAME);
  320.       exit_program(1);
  321.    }
  322.  
  323.    format_version = read_from_database();
  324.    if (format_version != DATABASE_FORMAT_VERSION) {
  325.       fprintf(stderr,
  326.          "Database format version (%d) is not the required one (%d) -- you must recompile the database.\n",
  327.          format_version, DATABASE_FORMAT_VERSION);
  328.       exit_program(1);
  329.    }
  330.  
  331.    abs_max_calls = read_from_database();
  332.    max_base_calls = read_from_database();
  333.  
  334.    n = read_from_database();
  335.    sprintf(major_database_version, "%d", n);
  336.    n = read_from_database();
  337.    sprintf(minor_database_version, "%d", n);
  338. }
  339.  
  340.  
  341. extern int read_from_database(void)
  342. {
  343.    int bar;
  344.  
  345.    bar = (fgetc(fp)&0xFF) << 8;
  346.    bar |= fgetc(fp)&0xFF;
  347.    return(bar);
  348. }
  349.  
  350.  
  351. extern void close_database(void)
  352. {
  353.    fclose(fp);
  354. }
  355.  
  356.  
  357. extern int parse_number(char junk[])
  358. {
  359.    int n;
  360.  
  361.    if (sscanf(junk, "%d", &n) != 1) return(0);
  362.    return(n);
  363. }
  364.  
  365.  
  366. extern void fill_in_neglect_percentage(char junk[], int n)
  367. {
  368.    sprintf(junk, "LEAST RECENTLY USED %d%% OF THE CALLS ARE:", n);
  369. }
  370.  
  371.  
  372. static FILE *call_list_file;
  373.  
  374. extern long_boolean open_call_list_file(call_list_mode_t call_list_mode, char filename[])
  375. {
  376.    if (call_list_mode == call_list_mode_abridging) {
  377.       call_list_file = fopen(filename, "r");
  378.    }
  379.    else {
  380.       call_list_file = fopen(filename, "w");
  381.    }
  382.  
  383.    if (!call_list_file) {
  384.       printf("Can't open call list file\n");
  385.       perror(filename);
  386.       return TRUE;
  387.    }
  388.    else
  389.       return FALSE;
  390. }
  391.  
  392.  
  393. extern char *read_from_call_list_file(char name[], int n)
  394. {
  395.    return (fgets(name, n, call_list_file));
  396. }
  397.  
  398.  
  399. extern void write_to_call_list_file(char name[])
  400. {
  401.    fputs(name, call_list_file);
  402.    fputs("\n", call_list_file);
  403. }
  404.  
  405.  
  406. extern long_boolean close_call_list_file(void)
  407. {
  408.    if (fclose(call_list_file)) {
  409.       printf("Can't close call list file\n");
  410.       return TRUE;
  411.    }
  412.    else
  413.       return FALSE;
  414. }
  415.